<?php
    session_start();
	if (isset($_SESSION['username']))
	{
		$login = true;
		$loguname = $_SESSION["username"];
	}
	else{
		$valid = false;
	}
	
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
	$path = 'users/'.$username.'/';
	$idfoto = $_GET["id"];
		require_once 'php\photo.php';
	$photor = photo::getPhoto($username,$idfoto);
	$photo = mysql_fetch_array($photor);
	if ($photo!=NULL){ 
		$valid = true;
		$_SESSION["lastpage"]='detail.php';
		$_SESSION["komenuname"] = $username;
		$_SESSION["komenidf"] = $idfoto;
	}
	$bolehDiakses = false;
	$hubFollow = photo::checkFollow($username,$loguname);
	if ($photo["HakAksesFoto"]==0){
			$bolehDiakses=true;	
	}
	else if ($photo["HakAksesFoto"]==1){
		if ($hubFollow==1||$hubFollow==3){
			$bolehDiakses=true;	
		}
	}
	else if ($photo["HakAksesFoto"]==2){
		if ($loguname==$username){
			$bolehDiakses=true;	
		}
	}
	
	$bolehDikomen = false;
	if ($photo["HakKomentarFoto"]==0){
			$bolehDikomen=true;	
	}
	else if ($photo["HakKomentarFoto"]==1){
		if ($hubFollow==1||$hubFollow==3){
			$bolehDikomen=true;	
		}
	}
	else if ($photo["HakKomentarFoto"]==2){
		if ($loguname==$username){
			$bolehDikomen=true;	
		}
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Footoo, Your Photo Detail</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="script/search.js"></script> 
<script type="text/javascript" src="script/visopt.js"></script> 
<link rel="stylesheet" type="text/css" href="style/footoo.css" />
<link rel="stylesheet" type="text/css" href="style/ftheader.css" />
<link rel="stylesheet" type="text/css" href="style/ftfooter.css" />
<link rel="stylesheet" type="text/css" href="style/ftcommoncontent.css" />
<link rel="icon" href="images/footoologomini.png" type="image/png" /> 
</head>

<body>
    <div id="headerwrapper">
    	<div id="headerbgfx"></div>
        <div id="footooheader">
        	<div id="footoologo">
            	<a href="#"></a>
            </div>
            <div id="logprofpicarea">
            <div id="profpicarea">
            	<table class="logouttable"><tr>
                <td class="logouttd">
                <?php
                    echo '<h1 class="username">'.$loguname.'</h1>';
                    echo '<br/>'.'15 follower, 22 followee'.'<br/>';
                    echo '<a class="logoutButton" href="php/LogOut.php">Log Out</a>'
                ?>
                </td>
                <td style="width:80px">
                <?php
                    require_once 'php\photo.php';
					$list = photo::getProfPic($loguname);
					$row = mysql_fetch_array($list);
					if ($row["FileFoto"] == NULL)
						echo '<img id="profpic" src = "'.'images/bg-photo.png'.'"/><br/>';
					else
					{
						$profpic = 'users/'.$loguname.'/'.$row["FileFoto"];
						echo '<img id="profpic" alt="profpic" src="users/'.$loguname.'/'.$row["FileFoto"].'" />';
					}
                ?>
                </td></tr></table>
            </div>
            </div>
        </div>
        <div id="footoomenu">
            <ul>
                <li><a href="home.php" >Home</a></li>
				<li><a href="profile.php?<?php echo $_SESSION[username];?>">Profile</a></li>
				<li><a href="wall.php?<?php echo $_SESSION[username];?>">Wall</a>
				<li><a class="current" href="fts.php">Photos</a></li>
				<li><a href="message.php">Message</a></li>
            </ul>
        </div>
    </div>
    <div id="headerfixer"></div>
    <div id="headermirror">
    	<div id="searchbox">
            <form action="search.php" method="post">
            	<select class="searchselect" name="tipe">
                	<option value="user">User</option>
                	<option value="photo">Photo</option>
                </select>
                <div class="searchbutton">
                    <input id="searchbutton" type="submit" value="" />
                </div>
                <div class="searchtext">
                <input id="searchtext" class="searchtext" type="text" name="searchtext" value="search" onfocus="filledTrue(this.id, 'search')" onblur="filledFalse(this.id, 'search')" />
            	</div>
            </form>		
        </div>
	</div>
    <div id="contentwrapper">
    	<div id="contento"><div id="contentphotodetail">
                <div id="photoshowarea">
                <?php
					if ($valid&&$bolehDiakses){
                        echo '<img id="bigphoto" alt="photo" src="'.$path.$photo["FileFoto"].'" />';
					}
					else if (!$valid){echo 'There is no photo to show';}
					else if (!$bolehDiakses){echo 'You have no authority to access this photo.';}
                ?>
                </div>
                <div id="photoshowdetail" class="showdetail">
                <?php	if ($valid&&$bolehDiakses){	?>
                	<div id="phdetailleft" class="detailleft"><?php
						echo 'Photo uploaded by : '.'<br/>';
						echo '<p class="bigspace"><a class="unameh2" href="#">'.$photo["USERNAME"].'</a></p><br/>';
                    	echo 'Uploaded on : '.$photo["tglUpload"].'<br/>';
                    	echo 'Taken on : '.$photo["tglAmbil"].'<br/><br/>';
                    ?>
                    <?php if ($username==$loguname){ ?>
                    	<div class="button180" onclick="show('photoshowedit');hide('photoshowdetail');">Edit Photo</div>
                    	<div class="button180" onclick="show('photoshowtag');hide('photoshowdetail');$('taglist').focus()">Edit Tag</div>
                    	<div class="button180" onclick="document.forms['avt'].submit();">Set As Avatar</div>
                    	<form name="avt" action="php/photoop.php" method="post">
                        	<input type="hidden" name="operation" value="avt"/>
                        </form>
                    <?php } ?>
                    </div>
                	
                    <div id="phdetailmiddle">
						<div id="toppanelcomment">
                        	<div id="nilai">
                            	<div class="nilaibar">
                                <?php $totalNilai = $photo["Likes"]+$photo["Dislikes"];?>
                                	<div class="nilaibarlikes" style="width:<?php echo 100*$photo["Likes"]/$totalNilai ?>%"></div>
                                	<div class="nilaibardislikes" style="width:<?php echo 100*$photo["Dislikes"]/$totalNilai ?>%"></div>
                                </div>
                            <?php
								 echo '<div class="nilaiteks">'.$photo["Likes"].' Likes, '.$photo["Dislikes"].' Dislikes</div>';
							?>
                            </div>
                            <div class="nilaicontrol">
                            	<div id="likebutt" class="likedislikebutt" onclick="document.forms['like'].submit();"></div>
                                <form name="like" action="php/photoop.php" method="post">
                                    <input type="hidden" name="operation" value="like"/>
                                </form>
                                <div id="dislikebutt" class="likedislikebutt" onclick="document.forms['dislike'].submit();"></div>
                                <form name="dislike" action="php/photoop.php" method="post">
                                    <input type="hidden" name="operation" value="dislike"/>
                                </form>
                            </div>
                    		<?php if ($bolehDikomen){ ?>
                            <br/><a class="commentbutton" href="javascript:void()" onclick="show('commentinput');$('cominput').focus();">Add Comment</a>
							<?php } ?>
                        </div>
                        <?php
							require_once 'php\photo.php';
							
							$listcomment = photo::getComment($username,$idfoto);
							$count = 0;
							while ($row = mysql_fetch_array($listcomment)){
								$count += 1;
						?>
                                <div class="boxcomment">
                                   	<div class="x" onclick="document.forms['delcomment<?php echo $row["IDKomentar"]; ?>'].submit();"></div>
                                    <form name="delcomment<?php echo $row["IDKomentar"]; ?>" action="php/photoop.php" method="post">
                                    	<input type="hidden" name="operation" value="delcomment"/>
                                    	<input type="hidden" name="idfoto" value="<?php echo $row["IDFoto"]; ?>"/>
                                    	<input type="hidden" name="komenname" value="<?php echo $row["Username"]; ?>"/>
                                    	<input type="hidden" name="idcomment" value="<?php echo $row["IDKomentar"]; ?>"/>
	                                </form>
                                    <div class="minava">
                                    <?php
										$avatarc = photo::getProfPic($row["Komentator"]);
										$ravatarc = mysql_fetch_array($avatarc);
										$pavatarc = 'users/'.$row["Komentator"].'/'.$ravatarc["FileFoto"];
                                        echo '<img class="minava" src="'.$pavatarc.'" />'
                                    ?>
                                    </div>
                                    <div class="commpl">
                                        <div style="text-align:left;padding-left:5px;"><a class="unameh3" href="#"><?php echo $row["Komentator"];?></a></div>
                                        <div class="comment"><?php echo $row["Komentar"];?></div>
                                        <div class="commenttime"><?php 
										/*$nowdate = new DateTime();
										$m = $nowdate->getTimestamp()-$row["Postedon"]; 
										$date = new DateTime($m);*/
										echo $row["Postedon"]; 
										?></div>
                                    </div>
                                </div>
						<?php }
						 ?>                 
                    	<div id="commentinput" class="boxcomment">
                        	<div class="minava">
                            <?php
                        		echo '<img class="minava" src="'.$profpic.'" />'
                            ?>
							</div>
                        	<div class="commpl">
                                <div style="text-align:left;padding-left:5px;font-size:12px;color:#333">
                                   Fill Your Comment
                                </div>
                                <div class="comment">
                                <form action="php/photoop.php" method="post">
                                    <input style="width:330px;" type="text" name="cominput" id="cominput" class="cominput" onblur="hide('commentinput');$(this.id).value='';" on/>
                                    <input type="hidden" name="operation" value="addcomment" />
                                    <input type="submit" name="submitcomment" style="display:none" />
                                </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                	<div id="phdetailright">
                    	<div class="teksh5">Description</div><?php echo '<p>'.$photo["Deskripsi"].'</p>'; ?>
                    	<div class="teksh5">Kategori</div><?php 
							require_once 'php/database.php';
							$katttext = database::query("SELECT * FROM Kategori WHERE IDKategori=' $photo[IDKategori]'");
							$kattext = mysql_fetch_array($katttext); 
							echo'<p>'.$kattext["Keterangan"].'</p>'; 
						?>
                    	<div class="teksh5">Keyword</div><?php echo '<p>Letak Keyword-keyword</p>'; ?>
                    	<div class="teksh5">Tagged On Photo</div><?php 
							require_once 'php/photo.php';
							$tag = photo::getTagged($username, $idfoto);
							$count = 0;
							while ($row = mysql_fetch_array($tag)){
								$count += 1;
								echo '<a class="unameh2" href="profile?'.$row["Usernametagged"].'">'.$row["Usernametagged"].'</a><br/>';
							}
							if ($count==0) echo '<p>None</p>';
							?>
                    </div>
				<?php	}	?>
                </div>
                
        <?php if ($username==$loguname){ ?>
                <div id="photoshowtag" class="showdetail">
                    <div id="tagshowleft" class="detailleft" style="border-right:solid 1px #999;">
                    	<div class="button180" onclick="document.forms['tagphoto'].submit();">Do Tag</div>
                    	<div class="button180" onclick="hide('photoshowtag');show('photoshowdetail');">Cancel</div>
                        <br/><br/>
                    	<div class="button180" onclick="document.forms[''].submit();">Remove All Tag</div>
                    </div>    
                    <div id="tagshowleft" class="detailbigright" style="border:none">
                        <form action="php/photoop.php" name="tagphoto" method="post">
                    		<input type="hidden" name="operation" value="tagphoto" />
                            <div class="editbox">Tagged User :
                        	<textarea id='taglist' class="dtextbox" type="text" name="taglist" rows="5"><?php
							require_once 'php/photo.php';
							$tag = photo::getTagged($username, $idfoto);
							while ($row = mysql_fetch_array($tag)){
								echo '@'.$row["Usernametagged"].', ';
							}  ?></textarea>
                        	</div>
                        </form>
                    	
                    </div>            	
                </div>
                
                <div id="photoshowedit" class="showdetail">
                    <div id="editshowleft" class="detailleft">
                    	<div class="button180" onclick="document.forms['editphoto'].submit();">Save Change</div>
                    	<div class="button180" onclick="hide('photoshowedit');show('photoshowdetail');">Cancel</div>
                        <br/><br/><br/><br/><br/>
                    	<div class="button180" onclick="document.forms['delphoto'].submit();">Delete Photo</div>
                        <form action="php/photoop.php" name="delphoto" method="post">
                    		<input type="hidden" name="operation" value="delphoto" />
                        </form>
                    </div>
                    <div id="editshowleft" class="detailbigright">
                    <form action="php/photoop.php" name="editphoto" method="post">
                    	<input type="hidden" name="operation" value="editdetail" />
                    	<div class="editbox">Description :
                        	<textarea class="dtextbox" type="text" name="idesc" rows="5"><?php echo $photo["Deskripsi"]; ?></textarea>
                        </div><div class="editbox"> Keyword :
                        	<textarea class="dtextbox" type="text" name="ikeyword" rows="2"><?php echo $photo["Keyword"]; ?></textarea>
						</div><div class="editbox">Category :
                            <select class="dtextbox" type="text" name="kategori" />
                            <?php
                                require_once 'php/photo.php';
                                $cate = photo::getCategory();
								$index = 1;
                                while ($crow = mysql_fetch_array($cate)){
                                    echo '<option value="'.$index.'" ';
									if ($photo["IDKategori"]==$index) echo ' selected="selected"';
									echo '>'.$crow["Keterangan"].'</option>';	
									$index++;
                                }
                            ?>
                            </select>
                        </div><div class="editbox">Comment :
                            <select class="dtextbox" name="hakkomen">
                              <option value="0" <?php if($photo["HakKomentarFoto"]==0) echo 'selected="selected"';?>>Everyone</option>
                              <option value="1" <?php if($photo["HakKomentarFoto"]==1) echo 'selected="selected"';?>>Follower Only</option>
                              <option value="2" <?php if($photo["HakKomentarFoto"]==2) echo 'selected="selected"';?>>Me Only</option>
                            </select>
                        </div><div class="editbox">Access :
                            <select class="dtextbox" name="hakakses">
                              <option value="0" <?php if($photo["HakAksesFoto"]==0) echo 'selected="selected"';?>>Everyone</option>
                              <option value="1" <?php if($photo["HakAksesFoto"]==1) echo 'selected="selected"';?>>Follower Only</option>
                              <option value="2" <?php if($photo["HakAksesFoto"]==2) echo 'selected="selected"';?>>Me Only</option>
                            </select>
                        </div>
					</form>
                    </div>
                </div>
         <?php } ?>
		</div></div>
    </div>

    <div id="footerwrapper">
    	<div id="footer">
            <div id="footerleft"><br/>
            	ft, ft logoes are trademark of ft company. <br/>
            	Copyright 2010 by : <br/>
                13508011 - M. Albadr Lutan Nasution <br/>
                13508019 - Gregorius Ronny Kaluge <br/>
                13508099 - Alwi Alfiansyah Ramdan <br/>
            </div>
            <div id="footermiddle">
            	
            </div>
            <div id="footerright">
            	
            </div>
        </div>
    </div>
</body>
</html>
